using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using Microsoft.SharePoint;
using System.Collections;
using System.Web;
using System.Reflection;
using IWCommunity.Common;


namespace IWCommunity.WebControls
{
    public class MembershipRequest
    {
        #region Class Level Variables

        protected string _UserName;
        protected string _FirstName;
        protected string _LastName;
        protected string _SiteURL;
        protected string _SiteName;
        protected string _Password;
        protected string _ChangePasswordURL;
        protected string _ThankYouURL;
        protected string _PasswordQuestionURL;
        protected string _UserEmail;
        protected string _PasswordQuestion;
        protected string _PasswordAnswer;

        #endregion

        #region Constructors

        public MembershipRequest()
        {
        }

        #endregion

        #region Properties

        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }

        public string FirstName
        {
            get
            {
                return _FirstName;
            }
            set
            {
                _FirstName = value;
            }
        }

        public string LastName
        {
            get
            {
                return _LastName;
            }
            set
            {
                _LastName = value;
            }
        }

        public string SiteURL
        {
            get
            {
                return _SiteURL;
            }
            set
            {
                _SiteURL = value;
            }
        }

        public string SiteName
        {
            get
            {
                return _SiteName;
            }
            set
            {
                _SiteName = value;
            }
        }

        public string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }

        public string ChangePasswordURL
        {
            get
            {
                return _ChangePasswordURL;
            }
            set
            {
                _ChangePasswordURL = value;
            }
        }

        public string ThankYouURL
        {
            get
            {
                return _ThankYouURL;
            }
            set
            {
                _ThankYouURL = value;
            }
        }

        public string PasswordQuestionURL
        {
            get
            {
                return _PasswordQuestionURL;
            }
            set
            {
                _PasswordQuestionURL = value;
            }
        }

        public string UserEmail
        {
            get
            {
                return _UserEmail;
            }
            set
            {
                _UserEmail = value;
            }
        }

        public string PasswordQuestion
        {
            get { return _PasswordQuestion; }
            set { _PasswordQuestion = value; }
        }

        public string PasswordAnswer
        {
            get { return _PasswordAnswer; }
            set { _PasswordAnswer = value; }
        }

        #endregion

        #region Methods
        public static void ApproveMembership(MembershipRequest request, SPWeb web)
        {
            Hashtable xsltValues;
            MembershipCreateStatus createStatus;
            string tempPassword = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(request.UserName))
                {
                    throw new Exception("User name must not be null or empty.");
                }

                if (string.IsNullOrEmpty(request.PasswordQuestion))
                {
                    throw new Exception("You must specify a password question.");
                }

                if (string.IsNullOrEmpty(request.PasswordAnswer))
                {
                    throw new Exception("You must specify a password answer.");
                }

                if (string.IsNullOrEmpty(request.UserEmail))
                {
                    throw new Exception("Email address must not be null or empty.");
                }
                //create account
                tempPassword = System.Web.Security.Membership.GeneratePassword(System.Web.Security.Membership.MinRequiredPasswordLength, System.Web.Security.Membership.MinRequiredNonAlphanumericCharacters);
                MembershipUser existingUser = System.Web.Security.Membership.GetUser(request.UserName);
                if (existingUser != null)
                {
                    System.Web.Security.Membership.DeleteUser(request.UserName, true);
                }
                System.Web.Security.Membership.CreateUser(request.UserName, tempPassword, request.UserEmail, request.PasswordQuestion, request.PasswordAnswer, true, out createStatus);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    xsltValues = new Hashtable(1);
                    xsltValues.Add("cks:MembershipRequest", request);

                    //add user to Visitors Group
                    //Roles.AddUserToRole(userName, roleName);

                    //email user to confirm that request is approved
                    Email.SendEmail(web, request.UserEmail, string.Format("{0}/_layouts/IWCommunity/emails/MembershipApproved.xslt", web.Url), xsltValues);
                }
                else
                {
                    throw new Exception("Error creating user: " + createStatus);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        public static void RejectMembership(MembershipRequest request, SPWeb web)
        {
            Hashtable xsltValues;

            try
            {
                xsltValues = new Hashtable();
                xsltValues.Add("cks:MembershipRequest", request);
                Email.SendEmail(web, request.UserEmail, string.Format("{0}/_layouts/IWCommunity/emails/MembershipRejected.xslt", web.Url), xsltValues);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        public static void SendPendingMembershipEmail(MembershipRequest request, SPWeb web)
        {
            Hashtable xsltValues;

            try
            {
                xsltValues = new Hashtable();
                xsltValues.Add("cks:MembershipRequest", request);
                Email.SendEmail(web, request.UserEmail, string.Format("{0}/_layouts/IWCommunity/emails/MembershipPending.xslt", web.Url), xsltValues);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        //public static void CopyToReviewList(MembershipRequest request)
        //{
        //    SPList reviewList;
        //    SPWeb web = null;
        //    SPListItem reviewItem = null;
        //    SPSite site = null;

        //    try
        //    {
        //        SPSecurity.RunWithElevatedPrivileges(delegate()
        //        {
        //            site = new SPSite(HttpContext.Current.Request.Url.ToString());
        //            web = site.OpenWeb();
        //            if (web != null)
        //            {

        //                site.AllowUnsafeUpdates = true;
        //                web.AllowUnsafeUpdates = true;
        //                reviewList = web.Lists[MembershipList.MEMBERSHIPREVIEWLIST];
        //                if (reviewList != null)
        //                {
        //                    reviewItem = reviewList.Items.Add();

        //                    reviewItem[MembershipReviewListFields.DATESUBMITTED] = DateTime.Now;
        //                    reviewItem[MembershipReviewListFields.EMAIL] = request.UserEmail;
        //                    reviewItem[MembershipReviewListFields.REQUESTID] = Guid.NewGuid();
        //                    reviewItem[MembershipReviewListFields.FIRSTNAME] = request.FirstName;
        //                    reviewItem[MembershipReviewListFields.LASTNAME] = request.LastName;
        //                    reviewItem[MembershipReviewListFields.STATUS] = MembershipStatus.Pending.ToString();
        //                    reviewItem[MembershipReviewListFields.USERNAME] = request.UserName;
        //                    reviewItem[MembershipReviewListFields.RECOVERPASSWORDQUESTION] = request.PasswordQuestion; ;
        //                    reviewItem[MembershipReviewListFields.RECOVERPASSWORDANSWER] = request.PasswordAnswer;
        //                    reviewItem.Update();
        //                    reviewList.Update();
        //                    MembershipRequest.SendPendingMembershipEmail(request, web);
        //                }
        //                else
        //                {
        //                    Logger.LogError("Unable to find Membership Review List", "MembershipRequest");
        //                }
        //            }
        //        });
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.LogError(ex);
        //    }
        //    finally
        //    {
        //        if (web != null)
        //        {
        //            web.Dispose();
        //        }

        //        if (site != null)
        //        {
        //            site.Dispose();
        //        }
        //    }
        //}

        #endregion
    }
}